/*
 * Copyright [2011] [Henry and Will]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 * */

package org.hnw.wte;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class WhereToEatActivity extends Activity {

	private ArrayList<Restaurant> restList;
	private Button startButton;
	private TextView restNameDisplay;
	private wteDataBase db;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        restNameDisplay = (TextView) this.findViewById(R.id.TextView1);
        
        restList = getRestaurantList();
        
        startButton = (Button) this.findViewById(R.id.Button1);
        startButton.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v) {
        		restNameDisplay.setText(randomRest());
        	}

        });
        
    }
    
	private ArrayList<Restaurant> getRestaurantList() {
		db = new wteDataBase(this);
		db.DBopen();
		ArrayList<Restaurant> list = db.DBretrieveAll();
		
		return list;
	}
	
	private String randomRest() {
		String result;
		result = restList.get((int) (Math.random() * restList.size())).getName();
		return result;
	}

	//define the menu button under this activity
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		MenuInflater myinf = getMenuInflater();
		myinf.inflate(R.menu.main_menu, menu);
		return true;
	}


	//define the function of the item of menu button
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId())
		{
			case R.id.restaurants:
				this.startActivity(new Intent(this, listview.class));
				break;
			
		}
		
		return true;
	}
	
	protected void onResume() {
		super.onResume();
        restList = getRestaurantList();
        return;
	}
	
}